package cn.cdeden.knowledge.service.impl;

import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.mybatis.validator.QueryHelpMybatisPlus;
import cn.cdeden.knowledge.domain.EnterpriseConfig;
import cn.cdeden.knowledge.domain.dto.EnterpriseConfigDto;
import cn.cdeden.knowledge.domain.dto.EnterpriseConfigQueryParam;
import cn.cdeden.knowledge.mapper.EnterpriseConfigMapper;
import cn.cdeden.knowledge.service.EnterpriseConfigService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author GW
* @date 2024-08-06
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = EnterpriseConfigService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class EnterpriseConfigServiceImpl extends ServiceImpl<EnterpriseConfigMapper, EnterpriseConfig> implements EnterpriseConfigService {

    // private final RedisUtils redisUtils;
    private final EnterpriseConfigMapper enterpriseConfigMapper;

//    @Override
//    public PageInfo<EnterpriseConfigDto> queryAll(EnterpriseConfigQueryParam query, Pageable pageable) {
//        IPage<EnterpriseConfig> queryPage = PageUtil.toMybatisPage(pageable);
//        IPage<EnterpriseConfig> page = enterpriseConfigMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
//        return ConvertUtil.convertPage(page, EnterpriseConfigDto.class,pageable);
//    }

    @Override
    public List<EnterpriseConfigDto> queryAll(EnterpriseConfigQueryParam query){
        return MapstructUtils.convert(enterpriseConfigMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), EnterpriseConfigDto.class);
    }

    @Override
    public EnterpriseConfig getById(String id) {
        return enterpriseConfigMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public EnterpriseConfigDto findById(String id) {
        return MapstructUtils.convert(getById(id), EnterpriseConfigDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insert(EnterpriseConfigDto resources) {
        EnterpriseConfig entity = MapstructUtils.convert(resources, EnterpriseConfig.class);
        return this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(EnterpriseConfigDto resources){
        EnterpriseConfig entity = MapstructUtils.convert(resources, EnterpriseConfig.class);
        boolean ret = this.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateByArray(List<EnterpriseConfigDto> enterpriseConfigDtoList){
        List<EnterpriseConfig> entity = MapstructUtils.convert(enterpriseConfigDtoList, EnterpriseConfig.class);
        boolean b = saveOrUpdateBatch(entity);
        // delCaches(resources.id);
        return b;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIds(Set<String> ids){
        // delCaches(ids);
        return this.removeByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(String id){
        Set<String> set = new HashSet<>(1);
        set.add(id);
        return this.deleteByIds(set);
    }

    /*
    private void delCaches(String id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<String> ids) {
        for (String id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<EnterpriseConfigDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (EnterpriseConfigDto enterpriseConfig : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("名称", enterpriseConfig.getName());
              map.put("值", enterpriseConfig.getValue());
              map.put("描述", enterpriseConfig.getDesc());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
